/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *
 */
package org.mizartools.dli;

public class TermAdjectiveRegistration extends ItemDefinition {

	private Loci loci = null;
	private Cluster cluster = null;
	private Term term = null;
	private Type type = null;
	
	private TermAdjectiveRegistration(){
		super(null);
	}
	
	public TermAdjectiveRegistration(Loci loci, Cluster cluster, Term term, Type type) throws DliException {
		super(null);
		if (loci == null || term == null){
			throw new DliException();
		}
		this.loci = loci;
		this.cluster = cluster;
		this.type = type;
		this.term = term;
    }

	public static ItemDefinition newInstance(String string) throws DliParseException {
		TermAdjectiveRegistration termAdjectiveRegistration = new TermAdjectiveRegistration();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in TermAdjectiveRegistration");
		}
		if (!string.substring(0, position).equals("funcreg")) {
			throw new DliParseException("["+string+"] is not valid in TermAdjectiveRegistration");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in TermAdjectiveRegistration");
		}
		if (string.startsWith("$loci")){
			termAdjectiveRegistration.loci = Loci.newInstance(string.substring(0, positionComma));
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in TermAdjectiveRegistration");
		}
		if (string.startsWith("$cluster")){
			termAdjectiveRegistration.cluster = Cluster.newInstance(string.substring(0, positionComma));
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		int positionFunc = string.indexOf(":func ");
		int positionSel = string.indexOf(":sel ");
		int positionAggr = string.indexOf(":aggr ");
		if (positionFunc == -1 && positionSel == -1 && positionAggr == -1) {
			throw new DliParseException("["+string+"] is not valid in TermAdjectiveRegistration");
		}
		if (positionComma == -1) {
			termAdjectiveRegistration.term = Term.newInstance(string);
		} else {
			termAdjectiveRegistration.term = Term.newInstance(string.substring(0, positionComma));
			string = string.substring(positionComma+1, string.length());
			if (!string.startsWith("$type")){
				throw new DliParseException("["+string+"] is not valid in TermAdjectiveRegistration");
			}
			termAdjectiveRegistration.type = Type.newInstance(string);
		}
		return termAdjectiveRegistration;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("funcreg(");
		sb.append(loci.toString());
		if (cluster != null){
			sb.append(",");
			sb.append(cluster.toString());
		}
		sb.append(",");
		sb.append(term.toString());
		if (type != null){
			sb.append(",");
			sb.append(type.toString());
		}
		sb.append(")");
		return sb.toString();
		
	}
	
}
